package com.service.impl;

import com.dao.StudentDao;
import com.pojo.Account;
import com.CustomTransaction;
import com.dao.AccountDao;
import com.pojo.StudentPojo;
import com.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountDao accountDao;
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private AccountService accountService;
    @CustomTransaction
    @Transactional(readOnly = false,propagation = Propagation.REQUIRED)
    public Integer transfer(String outName, String inName, Double money) {

            accountDao.inMoney(outName,money);
            accountDao.outMoney(inName,money);
            accountService.saveStu(new StudentPojo(null,"张三",18));
            return 1;
    }
    @Transactional(readOnly = false,propagation = Propagation.REQUIRED)
    public void saveStu(StudentPojo studentPojo) {
        studentDao.save(studentPojo);
        throw new RuntimeException();
    }


//    public void transfer(String outName, String inName, Double money) {
//        //开启事务
//        PlatformTransactionManager ptm = new DataSourceTransactionManager(dataSource);
//        //事务定义
//        TransactionDefinition td = new DefaultTransactionDefinition();
//        //事务状态
//        TransactionStatus ts = ptm.getTransaction(td);
//
//        accountDao.inMoney(outName,money);
//        int i = 1/0;
//        accountDao.outMoney(inName,money);
//
//        //提交事务
//        ptm.commit(ts);
//    }

}
